package com.woniu.controller;


import cn.hutool.core.lang.Snowflake;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;

/**
 * @Author: Gwj
 * @Date: 2024/7/2 19:28
 */
@RestController
@RequestMapping("/file")
@Slf4j
@Api(value = "图片上传接口", tags = "所有图片的功能")
public class FileController {
    @Resource
    private Snowflake snowflake;

    @PostMapping("/uploadAvatar")
    @ApiOperation(value = "上传头像")
    public String uploadAvatar(MultipartFile file) throws Exception {
        String filename = file.getOriginalFilename();
        long id = snowflake.nextId();
        String newFilename = id + "" + filename;
        log.info("上传头像图片名称：{}", filename);
        log.info("新的头像图片名称：{}", newFilename);
        // 存储路径：D:\\nginx\\nginx-1.26.1\\image\\
        // 上传图片到本地
        File newFile = new File("D:\\Program Files\\nginx-1.26.1\\html\\image\\Test", newFilename);
        try {
            file.transferTo(newFile);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return newFilename;
    }
}

